# Copyright (C) 2014-2015 Donovan Glover
# http://dglover.co/ | https://github.com/dglvr/rosarla

module Rosarla
	
	# Root functions --------------------------------------------------------------------
	
	# Return the combined value of multiple files (give an array)
	def self.combine(files)
		done = ""
		(files).each do |file|
			current_file = open(file.to_s)
			done += current_file.read
			current_file.close
		end
		return done
	end
	
	# Replace the content of <file> with <content>
	def self.replace(file, content)
		target = open(file, "w")
		target.write(content)
		target.close
	end
	
	# Returns an array of the current files in the given directory
	def self.get_files(source = "", type = "")
		return Dir.glob("#{source}/*#{type}")
	end
	
	# JavaScript / CSS Specific --------------------------------------------------------------------
	
	def self.build_js(params={})
		
		source = params[:source] || "assetsBuild/js"
		target = params[:target] || "assets/main.js"
		files = params[:files] || self.get_files(source, ".{js}")
		self.build(target, files)
	
	end
	
	def self.build_css(params={})
		
		source = params[:source] || "assetsBuild/css"
		target = params[:target] || "assets/design.css"
		files = params[:files] || self.get_files(source, ".{css}")
		self.build(target, files)
		
		
	end
	
	# Build Function --------------------------------------------------------------------
	
	def self.build(target, files)
		
		content = self.combine(files)
		self.replace(target, content)
		puts "Done! Your new file can be located at #{target}."
		
	end
	
end
